package com.iseastar.guojiang.task;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * 火箭侠 ------- 历史任务列表
 * Created by lidandan on 2018/1/17.
 */
public class TaskHistoryActivity extends BaseActivity2 {

    private TaskHistoryAdapter mAdapter;
    private ReqResult<ServiceTaskBean> result = null;
    private SListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().walletHistoryTask(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.wallet_history_task);
        super.findViewById();
        getAppTitle().setCommonTitle("历史任务");
        mListView = (SListView) findViewById(R.id.listView);
        mAdapter = new TaskHistoryAdapter(null, getContext());
        mListView.setAdapter(mAdapter);

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //如果有条件的话，就按条件来刷新，负责按照全部刷新
                AppHttp.getInstance().walletHistoryTask(1);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                //如果有条件的话，就按条件来请求，负责按照全部请求
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().walletHistoryTask(result.getPageNum() + 1);
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ServiceTaskBean item = (ServiceTaskBean) parent.getItemAtPosition(position);
                if (item != null) {
                    Intent intent = null;
                    //同城
                    if (Const.CourierTaskStatus.sameCityTask == item.getTaskType()) {
                        intent = new Intent(getContext(), CourierSameCityTaskDetailActivity.class);
                        intent.putExtra("item", item);
                        startActivity(intent);
                    } else {
                        intent = new Intent(getContext(), CourierTaskDetailActivity.class);
                        intent.putExtra("item", item);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.wallet_history_task:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, ServiceTaskBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (mAdapter == null) {
                                mAdapter = new TaskHistoryAdapter(null, getContext());
                                mListView.setAdapter(mAdapter);
                            }
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });

                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}

